<template>
    <h1>投资信息修改页面</h1>

    <table>
        <tr>
            <td>投资项目名称</td>
            <td>
                <input type="text" v-model="Invest.I_Name" >
            </td>
        </tr>

        <tr>
            <td>投资总额度</td>
            <td>
                <input type="text" v-model="Invest.I_Budget" >
            </td>
        </tr>
        
        <tr>
            <td colspan="2">
                <input type="button" class="btn btn-success" value="修改" @click="EditData" >
            </td>
        </tr>

    </table>

</template>

<script setup lang="ts">
import { ref,onMounted } from 'vue';
import axios from 'axios';
import { useRoute,useRouter } from 'vue-router';

const router = useRouter();
const route = useRoute();

const Invest = ref({
    I_Name:'',
    I_Budget:''
})

console.log(route.params.Id);

onMounted(()=>{
    SaveData();
})

const SaveData = ()=>{
    axios({
        method:'get',
        url:'http://localhost:56707/api/Invest/SaveData',
        params:{
            Id:route.params.Id
        }
    })
    .then((res)=>{
        console.log(res.data);
        Invest.value = res.data;
    })
    .catch((err)=>{
        console.log(err);
    })
}

const EditData = ()=>{

    axios({
        method:'Post',
        url:'http://localhost:56707/api/Invest/EditData',
        data:{
            I_Id:route.params.Id,
            I_Name:Invest.value.I_Name,
            I_Budget:Invest.value.I_Budget
        }
    })
    .then((res)=>{
        console.log(res.data);
        if(res.data>0){
            alert('修改成功');
            router.push('/InvestShow');
        }
        else if(res.data==-1){
            alert('当前投资项目,有人投资,无法修改');
            return;
        }
        else{
            alert('修改失败');
            return;
        }
    })
    .catch((err)=>{
        console.log(err);
    })
}

</script>
